package com.ChenYaRou.www.controller;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.ChenYaRou.www.dao.DetailedUserDao;
import com.ChenYaRou.www.dao.ShopDao;
import com.ChenYaRou.www.dao.UserDao;
import com.ChenYaRou.www.dao.impl.DetailedUserDaoImpl;
import com.ChenYaRou.www.dao.impl.ShopDaoImpl;
import com.ChenYaRou.www.dao.impl.UserDaoImpl;
import com.ChenYaRou.www.po.*;
import com.ChenYaRou.www.utils.GenerateSql;
import com.ChenYaRou.www.utils.JdbcUtil;
import com.ChenYaRou.www.utils.JsonBean;

import java.sql.SQLException;
import java.util.List;

import static com.ChenYaRou.www.constant.Constant.HAS_USED;

/**
 * @ClassName TakeOrdersServletController
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/1 21:53
 * @Version 1.0
 **/
public class TakeOrdersServletController {
    /**
     * 实现了单个订单下单 也实现了购物车多个下单.前端需要一一对应orders和他的ticket
     * 前端需要计算orders的价格,
     * 是否使用了ticket? 需要ticket判断 使用了传ticket 无使用传null √.
     * 如果资产不足,提示资产不足
     * @param orders
     * @return
     */
    public String takeOrders(List<Orders> orders, String userName, List<Ticket> ticket){
        DetailedUserDao detailedUserDao=new DetailedUserDaoImpl();
        if(orders!=null && !orders.isEmpty()){
            for (int i=0;i<orders.size();i++) {
                Orders order=orders.get(i);
                try {
                    if(userName==null){
                        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
                    }
                    UserDao userDao=new UserDaoImpl();
                    User user = userDao.select(userName);
                    DetailedUser detailedUser = detailedUserDao.select(user.getId());
                    if(detailedUser!=null){

                        if(detailedUser.getMoney()<order.getCost()){
                            return JsonBean.generateJson(JsonBeanConstant.NOT_GET,
                                    JsonBeanConstant.NOT_ENOUGH_MONEY,null);
                        }

                        //用户的金额足够
                        DetailedUser newDetailedUser = new DetailedUser();
                        newDetailedUser.setMoney(detailedUser.getMoney()-order.getCost());
                        String sqlDeleteUserMoney= GenerateSql.getInstance().update(newDetailedUser,detailedUser);

                        String sqlUseTicket=null;
                        if(ticket!=null && !ticket.isEmpty()){
                            Ticket ticketNew = ticket.get(i);
                            ticketNew.setState(HAS_USED);
                            sqlUseTicket = GenerateSql.getInstance().update(ticketNew,ticket.get(i));
                        }
                        String insertOrders = GenerateSql.getInstance().insert(order);
                        //工具里有判断是否为空,空则不执行.所以此处如果无使用抵用券也不影响
                        int result = JdbcUtil.getInstance().insertOrUpdateMore(sqlDeleteUserMoney,
                                sqlUseTicket, insertOrders);
                        if(result>0){
                            return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,null);
                        }
                    }
                } catch (SQLException | NoSuchFieldException | InstantiationException |
                        IllegalAccessException  e) {
                    e.printStackTrace();
                }
            }

            return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,null);
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }
}
